package com.atguigu.carousel.service.impl;

import com.atguigu.carousel.mapper.CarouselMapper;
import com.atguigu.carousel.service.CarouselService;
import com.atguigu.pojo.Carousel;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName:CarouselImpl
 * Package:com.atguigu.carousel.service.impl
 * Description:
 *
 * @Author 应永豪
 * @Create 2024/4/21 18:26
 * @Version 1.0
 */
@Service
@Slf4j
public class CarouselServiceImpl implements CarouselService {
    @Autowired
    private CarouselMapper carouselMapper;
    @Override
//    按照优先级查询数据库数据
//    用stream流进行内存数据切割，保留6条数据
    @Cacheable(value = "list.carousel",key = "#root.methodName",cacheManager = "cacheManagerDay")
    public R list() {
        QueryWrapper<Carousel> carouselQueryWrapper = new QueryWrapper<>();
        carouselQueryWrapper.orderByDesc("priority");
        List<Carousel> carousels = carouselMapper.selectList(carouselQueryWrapper);
        List<Carousel> collect = carousels.stream().limit(6).collect(Collectors.toList());
        log.info("CarouselImpl.list业务结束，结果为：{}",R.ok(collect));
        return R.ok(collect);
    }
}
